package co.fitstart.market.web.controller.course;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import co.fitstart.entity.market.DailyStatus;
import co.fitstart.entity.market.MarketBatchCourseware;
import co.fitstart.entity.market.MarketCourseRound;
import co.fitstart.entity.market.MarketDailyCourseware;
import co.fitstart.entity.market.MarketMonthlyCourseware;
import co.fitstart.entity.market.MarketWechatImage;
import co.fitstart.entity.market.TrainingRecord;
import co.fitstart.entity.user.User;
import co.fitstart.market.service.CoursewareService;
import co.fitstart.market.service.MarketCourseRoundService;
import co.fitstart.market.web.interceptor.MobileDetect;
import co.fitstart.market.web.interceptor.ParticipateDetect;
import co.fitstart.market.web.interceptor.Router;
import co.fitstart.market.web.interceptor.WechatAuthority;
import co.fitstart.market.web.resolver.Secure;

/**
 * 
 * @author houtao 、mohanlan
 *
 */
@Controller
@RequestMapping("/courses")
public class CourseController {
    
    @Autowired
    private MarketCourseRoundService marketCourseRoundService;
    
    @Autowired
    private CoursewareService coursewareService;
    
    /**
     * courses index page
     * 
     * @param user
     * @return
     */
    @RequestMapping(value="" , method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView monthlyList(@Secure User user){
        ModelAndView modelAndView = new ModelAndView("course/monthly-course-list");
        
        MarketCourseRound courseRound = marketCourseRoundService.load(user);
        modelAndView.addObject("courseRound", courseRound);
        
        List<MarketMonthlyCourseware> monthlies = coursewareService.listMonthlyCourseware(courseRound);
        modelAndView.addObject("monthlies", monthlies);
        
        MarketWechatImage wechatImage = coursewareService.getMarketWechatImage(courseRound.getCourse());
        modelAndView.addObject("wechatImage", wechatImage);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @param monthlyId
     * @return
     */
    @RequestMapping(value="/{monthlyId}/batches" , method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView batchList(@Secure User user, @PathVariable("monthlyId") Long monthlyId){
        ModelAndView modelAndView = new ModelAndView("course/batch-course-list");
        
        MarketMonthlyCourseware monthlyCourseware = coursewareService.loadMonthlyCourseware(monthlyId);
        modelAndView.addObject("monthlyCourseware", monthlyCourseware);
        
        List<MarketBatchCourseware> batches = coursewareService.listBatchCourseware(monthlyCourseware);
        modelAndView.addObject("batches", batches);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    @RequestMapping(value="/{monthlyId}/batches/{batchId}/finish-courseware" , method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView finishCourseware(@Secure User user,
                                         @PathVariable("monthlyId") Long monthlyId,
                                         @PathVariable("batchId") Long batchId){
        ModelAndView modelAndView = new ModelAndView("course/finish-courseware");
        
        MarketBatchCourseware batchCourseware = coursewareService.loadBatchCourseware(batchId);
        modelAndView.addObject("batchCourseware", batchCourseware);
        
        List<MarketDailyCourseware> dailyCoursewares = coursewareService.listDailyCourseware(batchCourseware, DailyStatus.FINISHED);
        modelAndView.addObject("dailyCoursewares", dailyCoursewares);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
    /**
     * courses detail page
     * 
     * @param courseId
     * @return
     */
    @RequestMapping(value="/{monthlyId}/batches/{courseId}" , method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView detail(@Secure User user, @PathVariable("courseId") Long courseId) {
        
        ModelAndView modelAndView = new ModelAndView("course/course-detail");
        
        MarketBatchCourseware batchCourseware = coursewareService.loadBatchCourseware(courseId);
        modelAndView.addObject("batchCourseware", batchCourseware);
        
        modelAndView.addObject("courseRound", batchCourseware.getMonthly().getCourseRound());
        modelAndView.addObject("user", user);
        
        TrainingRecord record = coursewareService.getTrainRecord(user, new Date());
        if(record != null) {
            modelAndView.setViewName("course/finished-course-list");
            
            List<MarketDailyCourseware> dailyCoursewares = coursewareService.listDailyCourseware(batchCourseware, DailyStatus.FINISHED);
            modelAndView.addObject("dailyCoursewares", dailyCoursewares);
        }
        
        return modelAndView;
    }
    
    /**
     * 
     * @param user
     * @param monthlyId
     * @return
     */
    @RequestMapping(value="/today" , method = RequestMethod.GET)
    @Router(targetUrl = "/required-wechat")
    @WechatAuthority
    @MobileDetect
    @ParticipateDetect
    public ModelAndView today(@Secure User user){
        ModelAndView modelAndView = new ModelAndView("course/unit/unit-index");
        
        MarketCourseRound courseRound = marketCourseRoundService.load(user);
        
        MarketMonthlyCourseware monthlyCourseware = coursewareService.loadMonthlyCourseware(courseRound);
        modelAndView.addObject("monthlyCourseware", monthlyCourseware);
        
        MarketBatchCourseware batchCourseware = coursewareService.loadBatchCourseware(monthlyCourseware);
        modelAndView.addObject("batchCourseware", batchCourseware);
        
        TrainingRecord record = coursewareService.getTrainRecord(user, new Date());
        modelAndView.addObject("record", record);
        
        MarketDailyCourseware dailyCourseware = null;
        
        if(record == null) {
            dailyCourseware = coursewareService.loadDailyCourseware(batchCourseware);
        } else {
            dailyCourseware = coursewareService.loadDailyCourseware(record.getDailyId());
        }
        modelAndView.addObject("dailyCourseware", dailyCourseware);
        
        modelAndView.addObject("user", user);
        
        return modelAndView;
    }
    
}
